#include "libfun.h"

int random(int start, int end)
{
	return start + rand()%(end - start + 1);
}

long get_cur_usec()
{
	struct timeval tv;
	gettimeofday(&tv, NULL);
	long lt = tv.tv_sec*1000 + tv.tv_usec/1000;
	return lt;
}

std::string general_time_str()
{
	time_t nowtime;
	struct tm *timeinfo;
	time(&nowtime);
	timeinfo = localtime(&nowtime);
	int year, month, day, hour, min, sec;
	year = 1900 + timeinfo->tm_year;
	month = 1 + timeinfo->tm_mon;
	day = timeinfo->tm_mday;
	hour = timeinfo->tm_hour;
	min = timeinfo->tm_min;
	sec = timeinfo->tm_sec;
	char buf[30] = {0};
	sprintf(buf,"%04d-%02d-%02d %02d:%02d:%02d",year,month,day,hour,min,sec);
	std::string ret_str = buf;
	return ret_str;	
}

std::string general_date_str()
{
	time_t nowtime;
	struct tm *timeinfo;
	time(&nowtime);
	timeinfo = localtime(&nowtime);
	int year, month, day;
	year = 1900 + timeinfo->tm_year;
	month = 1 + timeinfo->tm_mon;
	day = timeinfo->tm_mday;
	char buf[30] = { 0 };
	sprintf(buf, "%04d-%02d-%02d", year, month, day);
	std::string ret_str = buf;
	return ret_str;
}

std::string i2str(int n)
{
	std::ostringstream stream;
	stream<<n;  //n为int类型
	return stream.str();
}

std::string filter(std::vector<std::string>& v_words, std::string & s_str)
{
	std::vector<std::string>::iterator it;
 	for (it = v_words.begin(); it != v_words.end(); it++)
	{
	   std::string word = *it;
	   int pos;
	   pos = s_str.find(word);
	   while( pos != -1)
	   {
		   s_str.replace(pos, word.length(), "*");
		   pos = s_str.find(word);
	   }
	}
	return s_str;
}

int get_rate_by_continue_gang(int continue_times)
{
	if (continue_times <= 1) return 1;
	else if (continue_times == 2) return 2;
	else return 4;
}



